<?php
ob_start();
session_start();
if (!isset($_SESSION["userid"])){
	echo "No session register";
   header("location:index.php");	
}
else{
	$name = $_SESSION["name"];
	$imgurl =$_SESSION["imageurl"];
}
$userid = $_SESSION["userid"];
?>

<html>

<head>
<title>Homepage</title>
<LINK href="fbStyle.css" rel=stylesheet> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<?php include 'class_lib.php';?>

<script  type="text/javascript" language="javascript" >

/*scroll to position*/


function formToSubmit(){

   if(document.inserForm.newpost.value==""){
	alert("Please insert some text !");
 
   else{
	return document.insertForm;
	 
   }	   

}
function linkToUser(){
	
}
</script>


</head>

<body>

<div id="header">
<?php include 'header.php';?>
</div>

<div id="left">
      <div class="leftuserinfoframe">
            <img class="profileImage" src='<?php echo $imgurl ?>'></img>
            
             <?php 
             echo "<a class='profileName' href='personPage.php?useridperson=$userid'>$name</a>";
             ?>
      </div>
</div>

<div id="center">

<div class="insertframe">
   <form name="insertForm" action="homepage.php" method="Post">
   <h5 >Cập nhật trạng thái</h5>
   <textarea name="newpost" class="insertInput" rows= '4' ></textarea>
    ImageURL  <input type="text" name="urlimagelink"></input>
   <a href="#" onclick="document.insertForm.submit(); return yes;"  class="button">Post</a>
  </form>
</div>




<?php 

 $connect = new  Connection();


if (isset($HTTP_POST_VARS["newpost"])) {
	$newpost = $HTTP_POST_VARS["newpost"];
	if ($newpost != "") {
	$userid = $_SESSION["userid"];
	$imageurlpost = $HTTP_POST_VARS["urlimagelink"];
	date_default_timezone_set("Asia/Ho_Chi_Minh");
	$posttime = date('Y-m-d H:i:s');
	$postid = $posttime.$userid;
	
	$newpostquery = "INSERT INTO `fbdemodatabase`.`posts` (`post_id`, `userpostid`, `content`, `imageposturl`, `likesnumber`, `ispublic`, `posttime`) VALUES ('".$postid."', '".$userid."','".$newpost."', '".$imageurlpost."', '0', '1', '$posttime');";
	$postresult = $connect->requestdata($newpostquery);
    
	unset($GLOBALS['newpost']);
	header("location:homepage.php");
	}
	
}


$query = "Select * from user,posts where user.userid = posts.userpostid order by posttime desc";
$result = $connect->requestdata($query);
$number = mysql_num_rows($result);
$index = 0;
while ($index < $number) {
    $userpostid = mysql_result($result, $index,"userid");
	$userpostname = mysql_result($result, $index,"name");
	$profileurl = mysql_result($result, $index,"imageurl");
	

	$userpost = new User($userpostid, $userpostname, $profileurl);
	
	$content = mysql_result($result, $index,"content");
	$imagepost = mysql_result($result, $index,"imageposturl");
	$posttime = mysql_result($result, $index,"posttime");
	
	$friendid = mysql_result($result, $index,"friendpostid");
       	if ($friendid) {
       		$queryfriend = "select * from user where userid=$friendid";
            $resultfriend = $connect->requestdata($queryfriend);
            $firth = 0;
            $friendpostid = mysql_result($resultfriend, $firth,"userid");
            $friendpostname = mysql_result($resultfriend, $firth,"name");
            $friendprofileurl = mysql_result($resultfriend, $firth,"imageurl");
            
            $friendpost = new User($friendpostid, $friendpostname, $friendprofileurl);
            $thepost = new Post($userpostid, $userpost, $content, $imagepost, $posttime,$friendpost);
             
            $thepost->showPost();
             
       	}
       	else{
            $thepost = new Post($userpostid, $userpost, $content, $imagepost, $posttime,'');
 
            $thepost->showPost();
        }
	

	$index++;
}



?>



</div>

</body>
</html>